package com.slz3.business.module.memberManagement.memMember.steward;

import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.LocalDT;
import com.slz3.business.frame.util.Md5Utils;
import com.slz3.business.frame.util.RedisClient;
import com.slz3.business.module.memberManagement.memMember.domain.MemMember;
import com.slz3.business.module.memberManagement.memMember.enums.MemMemberSexFlag;
import com.slz3.business.module.memberManagement.memMember.enums.MemMemberSignInFlag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @FileName: MemMemberSynr
 * @Description:用户信息的新增。删除和修改同步消息类
 */

@Component
@Slf4j
public class MemMemberSynr {

    /**
     * 注册同步处理方法
     */
    public static void memMemberAdd(){

        Object user = null;
        Map<String,Object> memMemberMap = null;
        try {
            memMemberMap = (Map<String,Object>)Jedis.db0.leftPop(RK.USER_USERREGIST);
            log.info("消费消息:" + memMemberMap.toString());
            Steward.initNullField(memMemberMap);
            //消费逻辑
            saveMemBerUtilMethod(new MemMember(),memMemberMap);
            //处理完删除set集合
            RedisClient.redisTemplate.opsForSet().pop(RK.USER_USERREGIST+"Set");
        }catch (Exception e){
            //主数据set 集合
            Set members= Jedis.db0.getSet(RK.SAVE_ORDER.concat("Set"));
            Iterator iterator = members.iterator();
            //消费逻辑
            log.info("消费消息:" + members.toString());
            //遍历set集合
            while(iterator.hasNext()){
                memMemberMap =(Map<String,Object>) iterator.next();
                Steward.initNullField(memMemberMap);
                //根据map先获取订单信息并保存订单
                saveMemBerUtilMethod(new MemMember(),memMemberMap);
            }
            //删除set
            RedisClient.redisTemplate.opsForSet().pop(RK.USER_USERREGIST+"Set");
        }
    }

    /**
     * 用户存在则更新
     * @param memMember
     * @param map
     */
    private static void saveMemBerUtilMethod(MemMember memMember,Map<String,Object> map){
        Optional<MemMember> vo = Services.memMember.findById(map.get("id").toString());
        if(!vo.isPresent()){
            memMember.setIncrementId(Long.getLong(map.get("id").toString()));
            memMember.setUuid(UUID.fromString(map.get("memberId").toString()));
            memMember.setSerPhone(map.get("phone").toString());
            memMember.setSerPassWord(Md5Utils.encode(map.get("passWord").toString()));
            memMember.setSerUserName(map.get("userName").toString());
            memMember.setSerPicture(map.get("picture").toString());
            memMember.setSerGoldCoin(Integer.parseInt(map.get("goldCoin").toString()));
            memMember.setSerScore(Integer.parseInt(map.get("score").toString()));
            memMember.setSerSex("1".equals(map.get("sex").toString())? MemMemberSexFlag.NAN:MemMemberSexFlag.NV);
            memMember.setSerSignIn("1".equals(map.get("signIn").toString())? MemMemberSignInFlag.YIQIANDAO:MemMemberSignInFlag.WEIQIANDAO);
            LocalDateTime createTime = LocalDateTime.now();
            if(!StringUtils.isEmpty(map.get("createTime"))){
                createTime = LocalDT.parseStringToDateTime(map.get("createTime").toString(),"yyyy-MM-dd HH:mm:ss");
            }
            memMember.setCreateTime(createTime);
            LocalDateTime updateTime = LocalDateTime.now();
            if(!StringUtils.isEmpty(map.get("updateTime"))){
                updateTime = LocalDT.parseStringToDateTime(map.get("updateTime").toString(),"yyyy-MM-dd HH:mm:ss");
            }
            memMember.setLastModifyTime(updateTime);
            memMember.setSerAccountNo(map.get("accountNo").toString());
            Services.memMember.save(memMember);
        }else{
            memMember = vo.get();
            memMember.setUuid(UUID.fromString(map.get("memberId").toString()));
            memMember.setSerPhone(map.get("phone").toString());
            memMember.setSerPassWord(Md5Utils.encode(map.get("passWord").toString()));
            memMember.setSerUserName(map.get("userName").toString());
            memMember.setSerPicture(map.get("picture").toString());
            memMember.setSerGoldCoin(Integer.parseInt(map.get("goldCoin").toString()));
            memMember.setSerScore(Integer.parseInt(map.get("score").toString()));
            memMember.setSerSex("1".equals(map.get("sex").toString())? MemMemberSexFlag.NAN:MemMemberSexFlag.NV);
            memMember.setSerSignIn("1".equals(map.get("signIn").toString())?MemMemberSignInFlag.YIQIANDAO:MemMemberSignInFlag.WEIQIANDAO);
            memMember.setCreateTime(LocalDT.parseStringToDateTime(map.get("createTime").toString(),"yyyy-MM-dd HH:mm:ss"));
            memMember.setLastModifyTime(LocalDT.parseStringToDateTime(map.get("updateTime").toString(),"yyyy-MM-dd HH:mm:ss"));
            memMember.setSerAccountNo(map.get("accountNo").toString());
            Services.memMember.modify(memMember);
        }

    }
    
    
    
    





}